package com.eascs.dubbo.api.internal;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.api.internal.DownInfoService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanBkxyInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanGsInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanScToptencInfo;
import com.eascs.admin.o2o.loan.service.O2oLoanBkxyService;
import com.eascs.admin.o2o.loan.service.O2oLoanGsMainProductService;
import com.eascs.admin.o2o.loan.service.O2oLoanGsService;
import com.eascs.admin.o2o.loan.service.O2oLoanScTopTenCService;
import com.eascs.common.util.StringUtil;

/*
 *下游信息
 */
@Service
public class DownInfoServiceImpl implements DownInfoService{
	
	@Autowired
	O2oLoanScTopTenCService cService;
	
	@Autowired
	O2oLoanBkxyService o2oLoanBkxyService;
	@Autowired
	O2oLoanGsService gsService;
	
	@Autowired
	O2oLoanGsMainProductService productService;

	@Override
	public String saveDownSC(O2oLoanScToptencInfo o2oLoanScToptencInfo) {
		if(StringUtil.isBlank(o2oLoanScToptencInfo.getCusName())){
			return "客户名称不能为空";
		}
		if(StringUtil.isBlank(o2oLoanScToptencInfo.getSettlementDate())){
			return "结算账期不能为空";
		}
		if(StringUtil.isBlank(o2oLoanScToptencInfo.getSupplyQuantity())){
			return "供货量不能为空";
		}
		if(StringUtil.isBlank(o2oLoanScToptencInfo.getId())){
			int i = cService.insertEntity(o2oLoanScToptencInfo);
			if(i!=1){
				return "保存失败";
			}
		}else{
			O2oLoanScToptencInfo info = cService.getEntityById(o2oLoanScToptencInfo.getId());
			info.setCusName(o2oLoanScToptencInfo.getCusName());
			info.setSettlementDate(o2oLoanScToptencInfo.getSettlementDate());
			info.setSupplyQuantity(o2oLoanScToptencInfo.getSupplyQuantity());
			int i = cService.updateEntity(info);
			if(i!=0){
				return "修改失败";
			}
		}
		return null;
	}

	@Override
	public String saveDownBPJ(O2oLoanBkxyInfo loanBkxyInfo) {
		if (StringUtil.isBlank(loanBkxyInfo.getCustomerName())) {
			return "下游客户不能为空";
		}
		if (null == loanBkxyInfo.getSales()) {
			return "销售比例不能为空";
		}
		if (StringUtil.isBlank(loanBkxyInfo.getSettlement())) {
			return "结算方式不能为空";
		}
		if (StringUtil.isBlank(loanBkxyInfo.getPayment())) {
			return "回款情况不能为空";
		}
		if (StringUtil.isBlank(loanBkxyInfo.getId())) {
			loanBkxyInfo.setIsDelete(StatusContexts.O2O_NO);
			 o2oLoanBkxyService.insertEntity(loanBkxyInfo);
		} else {
			O2oLoanBkxyInfo bkxyInfo = o2oLoanBkxyService.getEntityById(loanBkxyInfo.getId());
			bkxyInfo.setCustomerName(loanBkxyInfo.getCustomerName());
			bkxyInfo.setLoanapplyId(loanBkxyInfo.getLoanapplyId());
			bkxyInfo.setPayment(loanBkxyInfo.getPayment());
			bkxyInfo.setSales(loanBkxyInfo.getSales());
			bkxyInfo.setSettlement(loanBkxyInfo.getSettlement());
			o2oLoanBkxyService.updateEntity(bkxyInfo);
		}
		return null;
	}

	@Override
	public String saveDownSG(O2oLoanGsInfo o2oLoanGsInfo) {
		O2oLoanGsInfo gsInfo = gsService.getLoanapplyNo(o2oLoanGsInfo.getLoanapplyNo());
		if (StringUtil.isBlank(o2oLoanGsInfo.getTwoBatch())) {
			return "二批商不能为空";
		}
		if (StringUtil.isBlank(o2oLoanGsInfo.getSupermarket())) {
			return "商超不能为空";
		}
		if (StringUtil.isBlank(o2oLoanGsInfo.getGroupPurch())) {
			return "团购不能为空";
		}
		if (StringUtil.isBlank(o2oLoanGsInfo.getRetail())) {
			return "零售终端不能为空";
		}
		if (null != gsInfo) {
			gsInfo.setTwoBatch(o2oLoanGsInfo.getTwoBatch());
			gsInfo.setSupermarket(o2oLoanGsInfo.getSupermarket());
			gsInfo.setGroupPurch(o2oLoanGsInfo.getGroupPurch());
			gsInfo.setRetail(o2oLoanGsInfo.getRetail());
			gsService.updateEntity(gsInfo);
		} else{
			gsService.insertEntity(o2oLoanGsInfo);
		}
		return null;
	}

	@Override
	public String deleteDownSC(String id) {
		int i = cService.delete(id);
		if(i<=0){
			return "删除失败";
		}
		return null;
	}

	@Override
	public String deleteDownBPJ(String id) {
		O2oLoanBkxyInfo loanBkxyInfo = o2oLoanBkxyService.getEntityById(id);
		loanBkxyInfo.setIsDelete(StatusContexts.O2O_YES);
		int i = o2oLoanBkxyService.updateEntity(loanBkxyInfo);
		if(i<=0){
			return "删除失败";
		}
		return null;
	}

}
